﻿using GreenXi.Help.Plc;
using NModbus;
using NModbus.Serial;
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Util;

namespace GreenXi.Help.Dam
{
    public class DamCom
    {
        // 获取量程对应转换范围（ART协议）
        static bool GetADModeRange(int lADMode, ref float lRangeTop, ref float lRangeButtom, ref string strUnit, long lRange)
        {
            bool bRet = false;

            switch (lADMode)
            {
                // 电压zz
                case DAM3000M.DAM3000M_VOLT_N15_P15: // ±15mV
                    lRangeTop = 15;
                    lRangeButtom = -15;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P15MV: // 0~15mV
                    lRangeTop = 15;
                    lRangeButtom = 0;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N20_P20: // ±20mV
                    lRangeTop = 20;
                    lRangeButtom = -20;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P20MV: // 0~20mV
                    lRangeTop = 20;
                    lRangeButtom = 0;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N30_P30: // ±30mV
                    lRangeTop = 30;
                    lRangeButtom = -30;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N50_P50: // ±50mV 
                    lRangeTop = 50;
                    lRangeButtom = -50;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N100_P100: // ±100mV 
                    lRangeTop = 100;
                    lRangeButtom = -100;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N150_P150: // ±150mV
                    lRangeTop = 150;
                    lRangeButtom = -150;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N500_P500: // ±500mV 
                    lRangeTop = 500;
                    lRangeButtom = -500;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P400MV: // 0~400mV 
                    lRangeTop = 400;
                    lRangeButtom = 0;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N400_P400MV: // -400~400mV 
                    lRangeTop = 400;
                    lRangeButtom = -400;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N300_P300V: // ±300V
                    lRangeTop = 300;
                    lRangeButtom = -300;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P300V: // 0V~300V
                    lRangeTop = 300;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N300_P0V: // -300V~0V
                    lRangeTop = 0;
                    lRangeButtom = -300;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N1_P1: // ±1V
                    lRangeTop = 1;
                    lRangeButtom = -1;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N25_P25: // ±2.5V 
                    lRangeTop = Convert.ToSingle(2.5);
                    lRangeButtom = Convert.ToSingle(-2.5);
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N5_P5: //±5V
                    lRangeTop = 5;
                    lRangeButtom = -5;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N10_P10: // ±10V 
                    lRangeTop = 10;
                    lRangeButtom = -10;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N0_P5: // 0~5V
                    lRangeTop = 5;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N0_P10: // 0~10V
                    lRangeTop = 10;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N10_P0V: // -10~0V
                    lRangeTop = 0;
                    lRangeButtom = -10;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P1: // 0-1V
                    lRangeTop = 1;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N0_P25: // 0-2.5V
                    if (lRange == 1)
                    {
                        lRangeTop = Convert.ToSingle(5);
                        lRangeButtom = 0;
                        strUnit = "V";
                        bRet = true;
                    }
                    else
                    {
                        lRangeTop = Convert.ToSingle(2.5);
                        lRangeButtom = 0;
                        strUnit = "V";
                        bRet = true;
                    }
                    break;

                case DAM3000M.DAM3000M_VOLT_N1_P5:// 1-5V
                    if (lRange == 1)
                    {
                        lRangeTop = 5;
                        lRangeButtom = 0;
                        strUnit = "V";
                        bRet = true;
                    }
                    else
                    {
                        lRangeTop = 5;
                        lRangeButtom = 1;
                        strUnit = "V";
                        bRet = true;
                    }
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P12:// 0-12V
                    lRangeTop = 12;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N12_P12V:// -12-12V
                    lRangeTop = 12;
                    lRangeButtom = -12;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P20:// 0-20V
                    lRangeTop = 20;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P15:// 0-15V
                    lRangeTop = 15;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N30_P30V:// -30～+30V
                    lRangeTop = 30;
                    lRangeButtom = -30;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N0_P30V:// 0～+30V
                    lRangeTop = 30;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N500_P500V:// -500～+500V
                    lRangeTop = 500;
                    lRangeButtom = -500;
                    strUnit = "V";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_VOLT_N0_P35:// 0～+35V
                    lRangeTop = 35;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N35_P0:// -35～0V
                    lRangeTop = 0;
                    lRangeButtom = -35;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N35_P35:// -35～35V
                    lRangeTop = 35;
                    lRangeButtom = -35;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N20_P0V:// -20～+0V
                    lRangeTop = 0;
                    lRangeButtom = -20;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N20_P20V:// -20～+20V
                    lRangeTop = 20;
                    lRangeButtom = -20;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P50:// 0 ～ 50V
                    lRangeTop = 50;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N50_P0:// -50 ～ 0V
                    lRangeTop = 0;
                    lRangeButtom = -50;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N50V_P50V:// -50 ～ 50V
                    lRangeTop = 50;
                    lRangeButtom = -50;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0V_P60V:// 0 ～ 60V
                    lRangeTop = 60;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N60V_P0V:// -60 ～ 0V
                    lRangeTop = 0;
                    lRangeButtom = -60;
                    strUnit = "V";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N60V_P60V://-60 ～ 60V
                    lRangeTop = 60;
                    lRangeButtom = -60;
                    strUnit = "V";
                    bRet = true;
                    break;
                // 电流
                case DAM3000M.DAM3000M_CUR_N0_P10: // 0~10mA
                    lRangeTop = 10;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N10_P10: // -10~10mA
                    lRangeTop = 10;
                    lRangeButtom = -10;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N20_P20: // ±20mA
                    lRangeTop = 20;
                    lRangeButtom = -20;
                    strUnit = "mA";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_CUR_N0_P20: //0~20mA
                    lRangeTop = 20;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_CUR_N4_P20: //4~20mA 
                    if (lRange == 1)
                    {
                        lRangeTop = 20;
                        lRangeButtom = 0;
                        strUnit = "mA";
                        bRet = true;
                    }
                    else
                    {
                        lRangeTop = 20;
                        lRangeButtom = 4;
                        strUnit = "mA";
                        bRet = true;
                    }
                    break;

                case DAM3000M.DAM3000M_CUR_N0_P22: //0~22mA 
                    lRangeTop = 22;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_CUR_N10_P10A:// -10～+10A
                    lRangeTop = 10;
                    lRangeButtom = -10;
                    strUnit = "A";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_CUR_N0_P10A:// 0～+10A
                    lRangeTop = 10;
                    lRangeButtom = 0;
                    strUnit = "A";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N10_P0A:// -10～+0A
                    lRangeTop = 0;
                    lRangeButtom = -10;
                    strUnit = "A";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_CUR_N50_P50A:// -50～+50A
                    lRangeTop = 50;
                    lRangeButtom = -50;
                    strUnit = "A";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_CUR_N0_P50A:// 0～+50A
                    lRangeTop = 50;
                    lRangeButtom = 0;
                    strUnit = "A";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P200: //0~200mA 
                    lRangeTop = 200;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N200_P0: //-200~0mA 
                    lRangeTop = 0;
                    lRangeButtom = -200;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N200_P200: //-200~200mA 
                    lRangeTop = 200;
                    lRangeButtom = -200;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P500: //0~500mA 
                    lRangeTop = 500;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N500_P0: //-500～0mA
                    lRangeTop = 0;
                    lRangeButtom = -500;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N500_P500: //-500～500mA
                    lRangeTop = 500;
                    lRangeButtom = -500;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P50: //0~50mA
                    lRangeTop = 50;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N50_P50: //-50～50mA
                    lRangeTop = 50;
                    lRangeButtom = -50;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P1500: //0~1500mA 
                    lRangeTop = 1500;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N1500_P0: //-1500 ～ 0mA
                    lRangeTop = 0;
                    lRangeButtom = -1500;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N1500_P1500: //-1500 ～ 1500mA
                    lRangeTop = 1500;
                    lRangeButtom = -1500;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P400UA: //0~400uA 
                    lRangeTop = 400;
                    lRangeButtom = 0;
                    strUnit = "uA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P1000: //0 ～ 1000mA
                    lRangeTop = 1000;
                    lRangeButtom = 0;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N1000_P0: //-1000 ～ 0mA
                    lRangeTop = 0;
                    lRangeButtom = -1000;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N1000_P1000: //-1000 ～ 1000mA
                    lRangeTop = 1000;
                    lRangeButtom = -1000;
                    strUnit = "mA";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P5A: //0～5A
                    lRangeTop = 5;
                    lRangeButtom = 0;
                    strUnit = "A";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N5_P0A: //-5～0A
                    lRangeTop = 0;
                    lRangeButtom = -5;
                    strUnit = "A";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N5_P5A: //-5～5A
                    lRangeTop = 5;
                    lRangeButtom = -5;
                    strUnit = "A";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N0_P3A: //0～3A
                    lRangeTop = 3;
                    lRangeButtom = 0;
                    strUnit = "A";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N3_P0A: //-3～0A
                    lRangeTop = 0;
                    lRangeButtom = -3;
                    strUnit = "A";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_CUR_N3_P3A: //-3～3A
                    lRangeTop = 3;
                    lRangeButtom = -3;
                    strUnit = "A";
                    bRet = true;
                    break;

                // 热电隅
                case DAM3000M.DAM3000M_TMC_J: // 0~1200℃
                    lRangeTop = 1200;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_K: // 0-1300℃
                    lRangeTop = 1300;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_K_EX: // -40-1300℃
                    lRangeTop = 1300;
                    lRangeButtom = -40;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_T: // -200-400℃
                    lRangeTop = 400;
                    lRangeButtom = -200;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_E: // 0-1000℃
                    lRangeTop = 1000;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_R: // 0-1700℃
                    lRangeTop = 1700;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_S: // 0-1768℃
                    lRangeTop = 1768;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_B: // 0-1800℃
                    lRangeTop = 1800;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_B_N: // 250-1800℃
                    lRangeTop = 1800;
                    lRangeButtom = 250;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_N: // 0-1300℃
                    lRangeTop = 1300;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_C: // 0-2090℃
                    lRangeTop = 2090;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_TMC_WRE: // 0-2310℃
                    lRangeTop = 2310;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                //  热电阻
                case DAM3000M.DAM3000M_RTD_PT100_385_N200_P600: // Pt100(-200～+600℃) a=0.00385
                    lRangeTop = 600;
                    lRangeButtom = -200;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_385_N100_P100: // Pt100(-100～+100℃) a=0.00385
                    lRangeTop = 100;
                    lRangeButtom = -100;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_385_N0_P100: // Pt100(0～+100℃) a=0.00385
                    lRangeTop = 100;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_385_N0_P200: // Pt100(0～+200℃) a=0.00385
                    lRangeTop = 200;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_385_N0_P600: // Pt100(0～+600℃) a=0.00385
                    lRangeTop = 600;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_3916_N200_P600: // Pt100(-200～+600℃) a=0.003916
                    lRangeTop = 600;
                    lRangeButtom = -200;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_3916_N100_P100: // Pt100(-100～+100℃) a=0.003916
                    lRangeTop = 100;
                    lRangeButtom = -100;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_3916_N0_P100: // Pt100(0～+100℃) a=0.003916
                    lRangeTop = 100;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_3916_N0_P200: // Pt100(0～+200℃) a=0.003916
                    lRangeTop = 200;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT100_3916_N0_P600: // Pt100(0～+600℃) a=0.003916
                    lRangeTop = 600;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT1000: // Pt1000(-200℃~600℃)
                    lRangeTop = 600;
                    lRangeButtom = -200;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_PT1000_N200_P850: // Pt1000(-200℃~850℃)
                    lRangeTop = 850;
                    lRangeButtom = -200;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_CU50: // Cu50(-50～+150℃)")
                    lRangeTop = 150;
                    lRangeButtom = -50;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_CU100: // Cu100(-50～+150℃)
                    lRangeTop = 150;
                    lRangeButtom = -50;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_BA1: // BA1	-200℃~650℃
                    lRangeTop = 650;
                    lRangeButtom = -200;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_BA2: // BA2	-200℃~650℃
                    lRangeTop = 650;
                    lRangeButtom = -200;
                    strUnit = "℃";
                    bRet = true;
                    break;

                case DAM3000M.DAM3000M_RTD_G53: // G53	-50℃～150℃		
                    lRangeTop = 150;
                    lRangeButtom = -50;
                    strUnit = "℃";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_RTD_103AT: // 103AT	-50℃～110℃		
                    lRangeTop = 110;
                    lRangeButtom = -50;
                    strUnit = "℃";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_NTC_10K: // NTC10K(0～100℃)
                    lRangeTop = 100;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_NTC_100K: // NTC100K(0～100℃)
                    lRangeTop = 100;
                    lRangeButtom = 0;
                    strUnit = "℃";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P500MV: // 0~500mV		
                    lRangeTop = 500;
                    lRangeButtom = 0;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P150MV: // 0~150 mV		
                    lRangeTop = 150;
                    lRangeButtom = 0;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P50MV: // 0~50 mV	
                    lRangeTop = 50;
                    lRangeButtom = 0;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_VOLT_N0_P100MV: // 0~100 mV	
                    lRangeTop = 100;
                    lRangeButtom = 0;
                    strUnit = "mV";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_RN0_50K: // 0～50KΩ
                    lRangeTop = 50000;
                    lRangeButtom = 0;
                    strUnit = "Ω";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_RN0_10K: // 0～10KΩ
                    lRangeTop = 10000;
                    lRangeButtom = 0;
                    strUnit = "Ω";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_RN0_200: // 0～200Ω
                    lRangeTop = 200;
                    lRangeButtom = 0;
                    strUnit = "Ω";
                    bRet = true;
                    break;
                case DAM3000M.DAM3000M_ACVOLT_N0_P35V: // 0～35V交流
                    lRangeTop = 35;
                    lRangeButtom = 0;
                    strUnit = "V";
                    bRet = true;
                    break;
            }

            return bRet;
        }
        [DllImport("msvcrt.dll")]
        public static extern int _getch();
        [DllImport("msvcrt.dll")]
        public static extern int _kbhit();
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
        const int MAX_PATH = 260;


        static IntPtr hDevice;                        //设备句柄
        static int lDeviceID = 1;                   //设备地址
        static ushort[] uADBuffer = new ushort[32];   //采集数据缓冲区
        static ushort lCHEnable = 0;                  //使能
        static byte[] byCHEnable = new byte[32];      //使能
        static int lCOMID = 5;                      //串口
        static int lBaud = 7;                       //波特率
        static int lChannelNum = 8;                 //通道号
        static ushort[] lMode = new ushort[32];       //AD量程
        static float lUpperRange = 10;                //量程最大值
        static float lLowRange = 0;                  //量程最小值
        static float fLsbType = 65535;                //码值范围
        static double fTmp = 0.0F;                    //码值转换后的浮值
        static string strUnit = "", strDevName = "", str = "";
        static int lShareRangeChannel = 0, bChEnable = 1, lRangeStretch = 1;
        static int nRangeCount = 6, ADDataBits = 2, Mode = 12;
        static int RangeAddr = 257, ChEnableAddr = 257, ADAddr = 257, IEnableType = 0;
        static StringBuilder strModeName = new StringBuilder(MAX_PATH);




        /// <summary>
        /// 打开压力传感器 
        /// </summary>
        public static void OpenPressureSensor()
        {

            #region 默认连接方式
            //获取串口
            lCOMID = Converter.GetIntPortByString(GlobalVariables.DataAcquisitionCardPortName);


            // 1、 创建设备(必须)**********************************************************************************
            hDevice = DAM3000M.DAM3000M_CreateDevice(lCOMID);


            if (hDevice == (IntPtr)0 || hDevice == (IntPtr)(-1))
            {
                Thread.Sleep(1000);
                return;
            }

            // 2、初始化设备(必须)**********************************************************************************
            if (!DAM3000M.DAM3000M_InitDevice(hDevice, lBaud, 8, 0, DAM3000M.DAM3000M_PARITY_NONE, 200, 0))
            {
                Console.WriteLine("初始化设备失败，退出");
                Thread.Sleep(1000);
                return;
            }

            // 使能全部通道进行采样（默认出厂是全部使能，如不需要修改，可忽略该过程）
            if (bChEnable == 1)// 如果支持通道使能
            {
                if (IEnableType == 1)//区分不同模块使用的接口函数
                {
                    for (int k = lChannelNum - 1; k >= 0; k--)
                    {
                        lCHEnable += Convert.ToUInt16(1 << k);
                    }
                    if (!DAM3000M.DAM3000M_WriteSingleReg(hDevice, lDeviceID, ChEnableAddr - 1, lCHEnable))
                    {
                        Console.WriteLine("设置通道使能失败");
                    }
                    else
                    {
                        Console.WriteLine("通道0使能地址={0}", ChEnableAddr);
                    }
                }
                else
                {
                    for (int k = lChannelNum - 1; k >= 0; k--)
                    {
                        byCHEnable[k] = 1;
                    }
                    if (!DAM3000M.DAM3000M_ForceMultiCoils(hDevice, lDeviceID, ChEnableAddr - 1, lChannelNum, byCHEnable))
                    {
                        Console.WriteLine("设置通道使能失败");
                    }
                }
            }

            //直接固定设备型号
            strDevName = "DAM-3158A";

            // 4、从配置文件中加载模块信息(非必须)**********************************************************************************
            // 包括板卡支持量程、通道使能是否支持、码值转换方式、读取数据的地址等
            // 这些信息都可以在产品的使用手册中确认。
            // 建议客户直接根据用户手册，确认模块支持的功能及协议定义。
            lChannelNum = 8;                                          //通道个数
            lShareRangeChannel = 0;                                   //区分多个通道共用一个量程和每个通道一个量程
            nRangeCount = 6;                                          //量程个数
            RangeAddr = 257;                                          //量程地址
            lRangeStretch = 1;                                        //量程是否拉伸
            fLsbType = Convert.ToSingle(65535.0);                     // 码值范围：0~4095、0~65535或者其它
            bChEnable = 1;                                            //是否可以使能通道
            ChEnableAddr = 257;                                       //使能通道地址
            ADAddr = 257;                                             //读取数据地址
            ADDataBits = 2;                                           //数据字节数
            IEnableType = 0;                                          //不同模块设置的量程功能码不同
            lMode[0] = 14;
            for (int index = 0; index < lChannelNum; index++)
            {
                lMode[index] = lMode[0];
            }

            if (!DAM3000M.DAM3000M_WriteMultiRegsUInt16(hDevice, lDeviceID, RangeAddr - 1, 1, lMode))
            {
                Console.WriteLine("设置量程失败");
                return;
            }
            //设置量程范围
            lUpperRange = 10;
            lLowRange = 0;
            #endregion

            GlobalVariables.IsPressureSensorOpen = true;

        }

        /// <summary>
        /// 打开压力传感器 
        /// </summary>
        public static void OpenPressureSensorByModbus()
        {

            ModbusFactory modbusFactory = new();
            GlobalVariables.ModbusSerialMaster3 = modbusFactory.CreateRtuMaster(GlobalVariables.DAM_3158A);
            GlobalVariables.IsPressureSensorOpen = false;
            if (GlobalVariables.DAM_3158A.IsOpen)
            {
                GlobalVariables.DAM_3158A.Close(); // 先关闭已打开的串口
            }

            GlobalVariables.ModbusSerialMaster3.Transport.ReadTimeout = 100;
            GlobalVariables.ModbusSerialMaster3.Transport.WriteTimeout = 200;//写超时
            GlobalVariables.ModbusSerialMaster3.Transport.Retries = 3;//尝试重复连接次数
            GlobalVariables.ModbusSerialMaster3.Transport.WaitToRetryMilliseconds = 10;//尝试重复连接间隔
            GlobalVariables.DAM_3158A.BaudRate = GlobalVariables.DataAcquisitionCardBaudRate;
            GlobalVariables.DAM_3158A.DataBits = GlobalVariables.DataAcquisitionCardDataBits;
            GlobalVariables.DAM_3158A.Parity = GlobalVariables.DataAcquisitionCardParity;
            GlobalVariables.DAM_3158A.StopBits = GlobalVariables.DataAcquisitionCardStopBits;
            GlobalVariables.DAM_3158A.PortName = GlobalVariables.DataAcquisitionCardPortName;
            try
            {
                GlobalVariables.DAM_3158A.Open();
                //设置量程
                GlobalVariables.ModbusSerialMaster3.WriteSingleRegister(1, 256, 14);
                //GlobalVariables.ModbusSerialMaster3.WriteSingleRegister(1, 257, 14);
                //GlobalVariables.ModbusSerialMaster3.WriteSingleRegister(1, 258, 14);
                //GlobalVariables.ModbusSerialMaster3.WriteSingleRegister(1, 259, 14);
                GlobalVariables.IsPressureSensorOpen = true;
            }
            catch (Exception e)
            {
                if (GlobalVariables.CallPolice == 0)
                {
                    GlobalVariables.CallPolice = (int)GlobalVariables.CallPoliceStatus.压力传感器断开连接;
                }
                Helpers.Logger.Instance.Error(e, "打开压力传感器catch:");
            }
        }

        /// <summary>
        /// 获取压力，同时给两个全局变量赋值
        /// </summary>
        /// <param name="port">获取压力的端口（对应1、2号压机</param>
        /// <returns>压力，单位KN</returns>
        public double GetPressure(int port)
        {

            PIDTool pIDTool = new();
            if (!GlobalVariables.IsPressureSensorOpen)
            {
                OpenPressureSensor();
            }
            //pIDTool.WriteConfigToTxt($"获取压力数据");
            //连接成功
            //TODO 这里读取不到会造成线程堵塞，使用限制时间的方法来规避过久阻塞线程
            var funcRes = DAM3000M.DAM3000M_ReadInputRegsUInt16(hDevice, lDeviceID, ADAddr - 1, lChannelNum, uADBuffer); ;

            if (funcRes)
            {
                //连接成功  
                fTmp = uADBuffer[port - 1] / fLsbType * (lUpperRange - lLowRange) + lLowRange;
                GlobalVariables.Card_Pressure1 = Math.Round((uADBuffer[0] / fLsbType * (lUpperRange - lLowRange) + lLowRange) * 20, 2);
                GlobalVariables.Card_Pressure2 = Math.Round((uADBuffer[1] / fLsbType * (lUpperRange - lLowRange) + lLowRange) * 20, 2);
            }
            else
            {

                if (port == 1)
                {
                    fTmp = GlobalVariables.Card_Pressure1 / 20;
                }
                else
                {
                    fTmp = GlobalVariables.Card_Pressure2 / 20;
                }
            }

            //压力保护
            if (fTmp * 20 > GlobalVariables.GreatestPressure || GlobalVariables.Card_Pressure1 > GlobalVariables.GreatestPressure || GlobalVariables.Card_Pressure2 > GlobalVariables.GreatestPressure)
            {
                if (GlobalVariables.CallPolice == 0)
                {
                    GlobalVariables.CallPolice = (int)GlobalVariables.CallPoliceStatus.运动控制卡断开连接;
                }
            }

            return Math.Round(fTmp * 20, 2);
        }


        /// <summary>
        /// 获取压力，同时给两个全局变量赋值
        /// </summary>
        /// <param name="port">获取压力的端口（对应1、2号压机</param>
        /// <returns>压力，单位KN</returns>
        public double GetPressureByModbus(int port)
        {
            //PIDTool pIDTool = new();
            try
            {
                PLC1 pLC = new();
                if (!GlobalVariables.IsPressureSensorOpen)
                {
                    return -9999;
                }
                var getInfo = GlobalVariables.ModbusSerialMaster3!.ReadInputRegisters(1, 256, 3);//读取前两个口的位移
                double k = GlobalVariables.MaximumPressureOfDataAcquisitionCard / 65536;//量程为0-200KN对应0-65536
                double pressure1 = Converter.Round(getInfo[0] * k, 2) + GlobalVariables.AcquirePressureCalibration1;
                double pressure2 = Converter.Round(getInfo[2] * k, 2) + GlobalVariables.AcquirePressureCalibration2;

                GlobalVariables.Card_Pressure1 = pressure1;
                GlobalVariables.Card_Pressure2 = pressure2;

                //压力保护
                if (GlobalVariables.Card_Pressure1 > GlobalVariables.GreatestPressure || GlobalVariables.Card_Pressure2 > GlobalVariables.GreatestPressure)
                {
                    //压力超了提示，并停止压机，这里的端口对应压机号
                    if (GlobalVariables.CallPolice == 0)
                    {
                        GlobalVariables.CallPolice = (int)GlobalVariables.CallPoliceStatus.压力达到限位;
                    }
                }

                return port == 1 ? pressure1 : pressure2;
            }
            catch (Exception ex)
            {
                ////这里的端口对应压机号
                if (GlobalVariables.CallPolice == 0)
                {
                    GlobalVariables.CallPolice = (int)GlobalVariables.CallPoliceStatus.压力传感器断开连接;
                }
                Helpers.Logger.Instance.Error(ex, "获取压力失败GetPressureByModbus");
                GlobalVariables.IsPressureSensorOpen = false;
                GlobalVariables.Card_Pressure1 = -9999;
                GlobalVariables.Card_Pressure2 = -9999;
                return -9999;
            }

        }





        public static void ReleaseDevice()
        {
            DAM3000M.DAM3000M_ReleaseDevice(hDevice);
            GlobalVariables.IsPressureSensorOpen = false;
        }
    }
}
